<?php

define('_LIST_',  0);
define('_COUNT_', 1);
define('_ELEMENT_',2);

class photo_gallery extends module
{
	
#-------------------------------------------------------------------------------------------------------------------------	

	var $id = 0;
	
#-------------------------------------------------------------------------------------------------------------------------	
	
	function content_init()
	{	
		global $common_data_server;
		
		$vopr_list = array();
		
		$this->id = intval($this->q_param['id']);
		$this->template = $common_data_server.'/module_templates/photo_gallery/'.$this->view_param['template'].'/';		
		
		if ( $this->id ) $this->ShowElement();
		else $this->ShowGallary();			
	}
	
#-------------------------------------------------------------------------------------------------------------------------	

	function ShowElement()
	{
		$template = $this->template.'element.vtmpl';
							
		if ( file_exists($template) )
		{
			$vopr = sql_select( $this->MakeSQL(_ELEMENT_) );
			
			$vtmpl = & new vlibTemplate($template);
			$vtmpl->setLoop('ELEMENT_LOOP', &$vopr);	
			$this->body    .= $vtmpl->grab();
			unset($vtmpl);
		}
		else $this->body .= "Template not found: ".$template;			
	}

#-------------------------------------------------------------------------------------------------------------------------	

	function ShowGallary()
	{
		global $common_htdocs_server;
		
		$template = $this->template.'photo_gallery.vtmpl';					
		if ( file_exists($template) )
		{
		
			$this->npp = $this->view_param['cols'] * $this->view_param['rows'];
			
			#navigation
			$count = sql_select( $this->MakeSQL( _COUNT_ ) );
			if ( $count[0]['COUNT'] > 1 )
			{
				$navigation = page_navigation($this->npp, $count[0]['COUNT'], "from" . $this -> tarea_id, $this->template."navigation.vtmpl");
			}	
			#end navigation
			
			$vopr_list = array();
			$vopr = sql_select( $this->MakeSQL( _LIST_ ) );
			$index = sizeof($vopr);
			
			if ( $index )
			{
				$row = 0;
				$col = 0;
				$vopr_list[$row]['ROW'] = array();
				
				foreach($vopr as $item)
				{
					if ( $col == $this->view_param['cols'] )
					{
						$col=0;
						$row++;
						$vopr_list[$row]['ROW'] = array();
		
					}	
					array_push($vopr_list[$row]['ROW'],$item);
					$col++;
					
				}
			}
			
			$vtmpl = & new vlibTemplate($template);
			$vtmpl->setVar('NAVIGATION', $navigation);
			$vtmpl->setVar('NUM', (2*$this->view_param['cols'] - 1));
			$vtmpl->setVar('REDIR', $redir);
			$vtmpl->setVar('TAREA',	$this->tarea_id);
			$vtmpl->setLoop('PG_LOOP', &$vopr_list);
					
			$this->body    .= $vtmpl->grab();
			unset($vtmpl);
		}
		else $this->body .= "Template not found: ".$template;

	}

#-------------------------------------------------------------------------------------------------------------------------	
	
	function MakeSQL( $mode = _LIST_ )
	{
		$limit = '';
		
		if ( $mode == _LIST_ )
		{
			$from = $this->q_param["from"] - 1;
			if( intval($from) < 0 ) $from = 0;
			$limit = ' LIMIT '.(string)($from*$this->npp).','.(string)($this->npp)." ";
		}
		
		$query = "	SELECT 
							".( $mode == _COUNT_ ? 'COUNT(*) AS COUNT' : '*' )." 
					FROM 
							PHOTO_GALLERY PG,
							CONTENT_MAP CM
					WHERE
							CM.INF_BLOCK_ID	=	'".$this->block_id."' AND 
							PG.CONTENT_ID	=	CM.CONTENT_ID AND 
							PG.VERSION		=	'".$this->version."' AND 
							PG.LANG_ID		=	'".$this->lang_id."'
							".( $mode == _ELEMENT_ ? " AND PG.CONTENT_ID = '".intval( $this->q_param['id'] )."'" : '' )."
					ORDER BY
							PG.LIST_ORDER ".
					$limit;
						
		return $query;				
	}
 	
#-------------------------------------------------------------------------------------------------------------------------
	
}

?>